package com.cw.jeeyt.controller.system;

import java.util.ArrayList;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import com.cw.jeeyt.api.dto.response.JsonResultDto;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cw.jeeyt.common.DataTable;
import com.cw.jeeyt.common.ResultData;
import com.cw.jeeyt.common.enums.EnumOperateFlag;
import com.cw.jeeyt.common.web.BaseController;

import com.cw.jeeyt.service.bo.system.SystemTemplateModuleListBo;
import com.cw.jeeyt.service.intf.system.SystemTemplateModuleListService;
import com.cw.jeeyt.service.directive.PageDictDirective;
import com.cw.jeeyt.service.directive.PermissionDirective;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.common.utils.PageUtil;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.mybatis.dto.PageParam;
import lombok.extern.slf4j.Slf4j;
/**
 * 模板模块清单控制层
 *
 * @author chenw
 * @date 2020-02-23 20:42:03
 */
@Controller
@RequestMapping( "/systemTemplateModuleList" )
@Slf4j
public class SystemTemplateModuleListController extends BaseController{
	@Resource
	private SystemTemplateModuleListService systemTemplateModuleListService;
	/**
	 * 跳转到模板模块清单列表查询页面
	 *
	 * @return
	 */
	@RequestMapping( value = "/query" )
	public ModelAndView query() {

		ModelAndView mv = new ModelAndView("system/systemTemplateModuleList_index");
		mv.addObject("permission", new PermissionDirective());
		mv.addObject("PageDict", new PageDictDirective());
		return mv;
	}
    /**
     * 跳转到模板模块清单列表查询页面
     *
     * @return
     */
    @RequestMapping( value = "/selectModuleList" )
    public ModelAndView selectModuleList(String selectType,String returnMethod) {

        ModelAndView mv = new ModelAndView("system/systemTemplateModuleList_select");
        mv.addObject("selectType", selectType);
        mv.addObject("returnMethod", returnMethod);
        return mv;
    }
	/**
	 * 加载DataTable列表数据
	 *
	 * @param dataTable datatable数据
	 * @return
	 */
	@ResponseBody
	@RequestMapping( value = "/listSystemTemplateModuleList")
	public ResultData listSystemTemplateModuleList(@RequestBody DataTable dataTable){

		SystemTemplateModuleListBo systemTemplateModuleListBo = JSONObject.parseObject(JSON.toJSONString(dataTable.queryCond),SystemTemplateModuleListBo.class);
		if(systemTemplateModuleListBo == null){
			systemTemplateModuleListBo = new SystemTemplateModuleListBo();
		}

		PageParam pageParam = new PageParam();
        pageParam.setPageNum(PageUtil.calPageNum(dataTable.getStart(),dataTable.getLength()));
        pageParam.setPageSize(dataTable.getLength());
		PageInfo<SystemTemplateModuleListBo> pageInfo = systemTemplateModuleListService.selectPage(systemTemplateModuleListBo,pageParam);
		return ResultData.builder()
            .draw(dataTable.getDraw())
            .data(CollectionUtil.isNotEmpty(pageInfo.getList())?pageInfo.getList():new ArrayList())
            .recordsTotal(pageInfo.getTotal())
            .recordsFiltered(pageInfo.getTotal())
            .build();
	}

	/**
	 * 跳转到模板模块清单表单页面
	 *
	 * @param operation 操作  add 新增 edit修改 view查看
	 * @return
	 * @throws Exception
	 */
	@RequestMapping( value = "/to-form" )
	public ModelAndView editEdit(Long id,String operation) {

		ModelAndView mv = new ModelAndView("system/systemTemplateModuleList_form");
		SystemTemplateModuleListBo systemTemplateModuleListBo = null;
		if(EnumOperateFlag.ADD.getCode().equals(operation)){
			systemTemplateModuleListBo = new SystemTemplateModuleListBo();
		}else{
			systemTemplateModuleListBo = systemTemplateModuleListService.selectByKey(id);
		}
		mv.addObject("systemTemplateModuleList", systemTemplateModuleListBo);
		mv.addObject("operation", operation);
		mv.addObject("PageDict", new PageDictDirective());
		mv.addObject("permission", new PermissionDirective());
		return mv;
	}
	/**
	 * 根据操作类型保存模板模块清单数据
	 *
	 * @param systemTemplateModuleListBo
	 * @param operation add 新增  edit 修改
	 */
	@ResponseBody
	@RequestMapping(value = "/saveSystemTemplateModuleList",method=RequestMethod.POST)
	public JsonResultDto saveSystemTemplateModuleList(SystemTemplateModuleListBo systemTemplateModuleListBo,String operation) {

		JsonResultDto jsonResultDto = new JsonResultDto(false);
		try{
			if(operation.equals(EnumOperateFlag.ADD.getCode())){
				systemTemplateModuleListService.addSystemTemplateModuleList(systemTemplateModuleListBo);
			}else if(operation.equals(EnumOperateFlag.EDIT.getCode())){
				systemTemplateModuleListService.modifySystemTemplateModuleList(systemTemplateModuleListBo);
			}
			jsonResultDto.setMsg("保存成功");
            jsonResultDto.setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
			jsonResultDto.setMsg("保存失败:" + e.getMessage());
		}
		return jsonResultDto;
	}
	/**
	 * 删除模板模块清单
	 *
	 * @param systemTemplateModuleListBo
	 * @throws Exception
	 */
	@RequestMapping( value = "/delete" )
	@ResponseBody
	public JsonResultDto delete(SystemTemplateModuleListBo systemTemplateModuleListBo) throws Exception{

		JsonResultDto jsonResultDto = new JsonResultDto(false);
		try{
			systemTemplateModuleListService.deleteSystemTemplateModuleList(systemTemplateModuleListBo);
			jsonResultDto.setMsg("删除成功");
            jsonResultDto.setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
			jsonResultDto.setMsg("删除失败:" + e.getMessage());
		}
		return jsonResultDto;
	}
}
